~ chicken-core (chicken-5) /manual/Module (chicken file posix)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken file posix)
5
6This module provides various operations on files and directories that
7are more POSIX-oriented than the generic higher-level operations from
8[[Module (chicken file)]].
9
10Note that the following definitions are not all available on non-UNIX
11systems like Windows. See below for Windows specific notes.
12
13All errors related to failing file-operations will signal a condition
14of kind {{(exn i/o file)}}.
15
16* New in CHICKEN 5.4.0: Errors caused by underlying C calls that
17 change errno will produce a condition object with an {{errno}}
18 property, which can be accessed with
19 {{(get-condition-property <the-condition-object> 'exn 'errno)}}.
20
21=== Constants
22
23==== File-control Commands
24
25<constant>fcntl/dupfd</constant><br>
26<constant>fcntl/getfd</constant><br>
27<constant>fcntl/setfd</constant><br>
28<constant>fcntl/getfl</constant><br>
29<constant>fcntl/setfl</constant>
30
31Operations used with {{file-control}}.
32
33'''NOTE''': On native Windows builds (all except cygwin), these are
34all defined as zero. The {{file-control}} procedure to use these with
35is also unimplemented and will raise an error when called.
36
37==== File positions
38
39<constant>seek/cur</constant><br>
40<constant>seek/set</constant><br>
41<constant>seek/end</constant><br>
42
43File positions for {{set-file-position!}}.
44
45==== Standard I/O file-descriptors
46
47<constant>fileno/stdin</constant><br>
48<constant>fileno/stdout</constant><br>
49<constant>fileno/stderr</constant>
50
51Standard I/O file descriptor numbers, used with procedures
52such as {{open-input-file*}} which take file descriptors.
53
54==== Open flags
55
56<constant>open/rdonly</constant><br>
57<constant>open/wronly</constant><br>
58<constant>open/rdwr</constant><br>
59<constant>open/read</constant><br>
60<constant>open/write</constant><br>
61<constant>open/creat</constant><br>
62<constant>open/append</constant><br>
63<constant>open/excl</constant><br>
64<constant>open/noctty</constant><br>
65<constant>open/nonblock</constant><br>
66<constant>open/trunc</constant><br>
67<constant>open/sync</constant><br>
68<constant>open/fsync</constant><br>
69<constant>open/binary</constant><br>
70<constant>open/text</constant>
71
72Open flags used with the {{file-open}} procedure. {{open/read}} is a
73convenience synonym for {{open/rdonly}}, as is {{open/write}}
74for {{open/wronly}}.
75
76'''NOTE''': On native Windows builds (all except cygwin),
77{{open/noctty}}, {{open/nonblock}}, {{open/fsync}} and {{open/sync}}
78are defined as zero because the corresponding flag doesn't exist.
79This means you can safely add these to any set of flags when opening a
80file or pipe, but it simply won't have an effect.
81
82==== Open flags for create-pipe
83
84<constant>open/noinherit</constant>
85
86This variable is a mode value for {{create-pipe}}. Useful when
87spawning a child process on Windows. On UNIX it is defined as zero,
88so you can safely pass it there, but it will have no effect.
89
90
91==== Permission bits
92
93<constant>perm/irusr</constant><br>
94<constant>perm/iwusr</constant><br>
95<constant>perm/ixusr</constant><br>
96<constant>perm/irgrp</constant><br>
97<constant>perm/iwgrp</constant><br>
98<constant>perm/ixgrp</constant><br>
99<constant>perm/iroth</constant><br>
100<constant>perm/iwoth</constant><br>
101<constant>perm/ixoth</constant><br>
102<constant>perm/irwxu</constant><br>
103<constant>perm/irwxg</constant><br>
104<constant>perm/irwxo</constant><br>
105<constant>perm/isvtx</constant><br>
106<constant>perm/isuid</constant><br>
107<constant>perm/isgid</constant>
108
109Permission bits used with, for example, {{file-open}}.
110
111'''NOTE''': On native Windows builds (all except cygwin),
112{{perm/isvtx}}, {{perm/isuid}} and {{perm/isgid}} are defined as zero
113because the corresponding permission doesn't exist. This means you
114can safely add these to any set of flags when opening a file or pipe,
115but it simply won't have an effect.
116
117
118=== Information about files
119
120==== directory?
121
122<procedure>(directory? FILE)</procedure>
123
124Returns {{#t}} if {{FILE}} designates a directory. Otherwise, it returns {{#f}}.
125{{FILE}} may be a pathname, a file-descriptor or a port object.
126
127
128==== file-type
129
130<procedure>(file-type FILE [LINK [ERROR]])</procedure>
131
132Returns the file-type for {{FILE}}, which should be a filename, a file-descriptor
133or a port object. If {{LINK}} is given and true, symbolic-links are
134not followed:
135
136 regular-file
137 directory
138 fifo
139 socket
140 symbolic-link
141 character-device
142 block-device
143
144Note that not all types are supported on every platform.
145If {{ERROR}} is given and false, then {{file-type}} returns {{#f}} if the file does not exist;
146otherwise, it signals an error.
147
148==== character-device?
149==== block-device?
150==== socket?
151
152<procedure>(character-device? FILE)</procedure><br>
153<procedure>(block-device? FILE)</procedure><br>
154<procedure>(socket? FILE)</procedure>
155
156These procedures return {{#t}} if {{FILE}} given is of the
157appropriate type. {{FILE}} may be a filename, a file-descriptor or a port object.
158Note that these operations follow symbolic links. If the file does
159not exist, {{#f}} is returned.
160
161==== regular-file?
162
163<procedure>(regular-file? FILE)</procedure>
164
165Returns true, if {{FILE}} names a regular file (not a directory, socket, etc.) This operation follows symbolic links; use either {{symbolic-link?}} or {{file-type}} if you need to test for symlinks. {{FILE}} may refer to a filename, file descriptor or ports object.
166
167
168=== Fifos
169
170==== create-fifo
171
172<procedure>(create-fifo FILENAME [MODE])</procedure>
173
174Creates a FIFO with the name {{FILENAME}} and the permission bits
175{{MODE}}, which defaults to
176
177<enscript highlight=scheme>
178 (+ perm/irwxu perm/irwxg perm/irwxo)
179</enscript>
180
181'''NOTE''': On native Windows builds (all except cygwin), this
182procedure is unimplemented and will raise an error.
183
184==== fifo?
185
186<procedure>(fifo? FILE)</procedure>
187
188Returns {{#t}} if {{FILE}} names a FIFO. {{FILE}} may be a filename,
189a port or a file-descriptor.
190
191
192=== Retrieving file attributes
193
194==== file-access-time
195==== file-change-time
196==== file-modification-time
197<procedure>(file-access-time FILE)</procedure><br>
198<procedure>(file-change-time FILE)</procedure><br>
199<procedure>(file-modification-time FILE)</procedure>
200
201Returns time (in seconds) of the last access, inode change or content
202modification of {{FILE}}, respectively. {{FILE}} may be a filename, a
203file-descriptor or a file-backed port. If the file does not exist, an
204error is signaled.
205
206==== set-file-times!
207
208<procedure>(set-file-times! FILE [MTIME [ATIME]])</procedure>
209
210Sets the time of last modification {{MTIME}} and/or time of last
211access {{ATIME}} (in seconds) for {{FILE}}. {{FILE}} may be a
212filename, a file-descriptor or a file-backed port. If the file does
213not exist, an error is signaled.
214
215If neither {{MTIME}} nor {{ATIME}} is supplied, the current time is
216used. If only {{MTIME}} is supplied, {{ATIME}} will be set to the
217same value. If an argument is {{#f}}, it will not be changed.
218
219Consequently, if only {{MTIME}} is passed and it is {{#f}}, {{ATIME}}
220is assumed to be {{#f}} as well and neither will be changed.
221
222
223==== file-stat
224
225<procedure>(file-stat FILE [LINK])</procedure>
226
227Returns a 13-element vector with the following contents:
228
229<table>
230<tr><th>index</th>
231 <th>value</th>
232 <th>field</th>
233 <th>notes</th></tr>
234<tr><td>0</td>
235 <td>inode number</td>
236 <td>{{st_ino}}</td>
237 <td></td></tr>
238<tr><td>1</td>
239 <td>mode</td>
240 <td>{{st_mode}}</td>
241 <td>bitfield combining file permissions and file type</td></tr>
242<tr><td>2</td>
243 <td>number of hard links</td>
244 <td>{{st_nlink}}</td>
245 <td></td></tr>
246<tr><td>3</td>
247 <td>UID of owner</td>
248 <td>{{st_uid}}</td>
249 <td>as with {{file-owner}}</td></tr>
250<tr><td>4</td>
251 <td>GID of owner</td>
252 <td>{{st_gid}}</td>
253 <td></td></tr>
254<tr><td>5</td>
255 <td>size</td>
256 <td>{{st_size}}</td>
257 <td>as with {{file-size}}</td></tr>
258<tr><td>6</td>
259 <td>access time</td>
260 <td>{{st_atime}}</td>
261 <td>as with {{file-access-time}}</td></tr>
262<tr><td>7</td>
263 <td>change time</td>
264 <td>{{st_ctime}}</td>
265 <td>as with {{file-change-time}}</td></tr>
266<tr><td>8</td>
267 <td>modification time</td>
268 <td>{{st_mtime}}</td>
269 <td>as with {{file-modification-time}}</td></tr>
270<tr><td>9</td>
271 <td>parent device ID </td>
272 <td>{{st_dev}}</td>
273 <td>ID of device on which this file resides</td></tr>
274<tr><td>10</td>
275 <td>device ID</td>
276 <td>{{st_rdev}}</td>
277 <td>device ID for special files (i.e. the raw major/minor number)</td></tr>
278<tr><td>11</td>
279 <td>block size</td>
280 <td>{{st_blksize}}</td>
281 <td></td></tr>
282<tr><td>12</td>
283 <td>number of blocks allocated</td>
284 <td>{{st_blocks}}</td>
285 <td></td></tr>
286</table>
287
288On Windows systems, the last 4 values are undefined.
289
290By default, symbolic links are followed and
291the status of the referenced file is returned;
292however, if the optional argument {{LINK}} is given and
293not {{#f}}, the status of the link itself is returned.
294
295{{FILE}} may be a filename, port or file-descriptor.
296
297Note that for very large files, the {{file-size}} value may be an
298inexact integer.
299
300==== file-position
301
302<procedure>(file-position FILE)</procedure>
303
304Returns the current file position of {{FILE}}, which should be a
305port or a file-descriptor.
306
307==== file-size
308
309<procedure>(file-size FILE)</procedure>
310
311Returns the size of the file designated by {{FILE}}. {{FILE}}
312may be a filename, a file-descriptor or a port object. If the file does not exist,
313an error is signaled. Note that for very large files, {{file-size}} may
314return an inexact integer.
315
316==== file-owner
317
318<procedure>(file-owner FILE)</procedure>
319
320Returns the user-id of {{FILE}} (an exact integer). {{FILE}} may be a
321filename, a file-descriptor or a port object.
322
323==== set-file-owner!
324
325<procedure>(set-file-owner! FILE UID)</procedure>
326<procedure>(set! (file-owner FILE) UID)</procedure>
327
328Changes the ownership of {{FILE}} to user-id {{UID}} (which should be
329an exact integer) using the {{chown()}} system call. {{FILE}} may be
330a filename, a file-descriptor or a port object.
331
332'''NOTE''': On native Windows builds (all except cygwin), this
333procedure is unimplemented and will raise an error.
334
335==== file-group
336
337<procedure>(file-group FILE)</procedure>
338
339Returns the group-id of {{FILE}}. {{FILE}} may be a filename, a
340file-descriptor or a port object.
341
342==== set-file-group!
343
344<procedure>(set-file-group! FILE GID)</procedure>
345<procedure>(set! (file-group FILE) GID)</procedure>
346
347Changes the group ownership of {{FILE}} to group-id {{GID}} (which
348should be an exact integer) using the {{chgrp()}} system call.
349{{FILE}} may be a filename, a file-descriptor or a port object.
350
351'''NOTE''': On native Windows builds (all except cygwin), this
352procedure is unimplemented and will raise an error.
353
354
355==== file-permissions
356
357<procedure>(file-permissions FILE)</procedure>
358
359Returns the permission bits for {{FILE}}. You can test this value
360by performing bitwise operations on the result and the {{perm/...}}
361values. {{FILE}} may be a filename, a file-descriptor or a port object.
362
363==== set-file-permissions!
364
365<procedure>(set-file-permissions! FILE MODE)</procedure>
366<procedure>(set! (file-permissions FILE) MODE)</procedure>
367
368Changes the current permission bits for {{FILE}} to {{MODE}} using the
369{{chmod()}} system call. The {{perm/...}} variables contain the
370various permission bits and can be combinded with the {{bitwise-ior}}
371procedure. {{FILE}} may be a filename, a file-descriptor or a port
372object, {{MODE}} should be a fixnum.
373
374
375==== file-truncate
376
377<procedure>(file-truncate FILE OFFSET)</procedure>
378
379Truncates the file {{FILE}} to the length {{OFFSET}},
380which should be an integer. If the file-size is smaller or equal to
381{{OFFSET}} then nothing is done. {{FILE}} should be a filename,
382a file-descriptor or a port object.
383
384'''NOTE''': On native Windows builds (all except cygwin), this
385procedure is unimplemented and will raise an error.
386
387==== set-file-position!
388
389<procedure>(set-file-position! FILE POSITION [WHENCE])</procedure><br>
390<procedure>(set! (file-position FILE) POSITION)</procedure>
391
392Sets the current read/write position of {{FILE}} to
393{{POSITION}}, which should be an exact integer. {{FILE}}
394should be a port or a file-descriptor. {{WHENCE}} specifies
395how the position is to interpreted and should be one of the values
396{{seek/set, seek/cur}} and {{seek/end}}. It defaults to
397{{seek/set}}.
398
399Exceptions: {{(exn bounds)}}, {{(exn i/o file)}}
400
401
402==== file-creation-mode
403
404<procedure>(file-creation-mode [MODE])</procedure>
405
406Returns the initial file permissions used for newly created files
407(as with {{umask(2)}}). If {{MODE}} is supplied, the mode is
408changed to this value. You can set the mode by executing
409
410 (set! (file-creation-mode) MODE)
411
412or
413
414 (file-creation-mode MODE)
415
416where {{MODE}} is a bitwise combination of one or more of
417the {{perm/...}} flags.
418
419
420=== Hard and symbolic links
421
422==== file-link
423
424<procedure>(file-link OLDNAME NEWNAME)</procedure>
425
426Creates a hard link from {{OLDNAME}} to {{NEWNAME}} (both strings).
427
428
429==== symbolic-link?
430
431<procedure>(symbolic-link? FILE)</procedure>
432
433Returns true, if {{FILE}} names a symbolic link. If no such file exists, {{#f}}
434is returned. This operation does not follow symbolic links itself.
435{{FILE}} could be a filename, file descriptor or port object.
436
437==== create-symbolic-link
438
439<procedure>(create-symbolic-link OLDNAME NEWNAME)</procedure>
440
441Creates a symbolic link with the filename {{NEWNAME}} that points
442to the file named {{OLDNAME}}.
443
444'''NOTE''': On native Windows builds (all except cygwin), this
445procedure is unimplemented and will raise an error.
446
447==== read-symbolic-link
448
449<procedure>(read-symbolic-link FILENAME [CANONICALIZE])</procedure>
450
451Returns the filename to which the symbolic link {{FILENAME}} points.
452If {{CANONICALIZE}} is given and true, then symbolic links are
453resolved repeatedly until the result is not a link.
454
455'''NOTE''': On native Windows builds (all except cygwin), this
456procedure is unimplemented and will raise an error.
457
458
459=== File descriptors and low-level I/O
460
461==== duplicate-fileno
462
463<procedure>(duplicate-fileno OLD [NEW])</procedure>
464
465If {{NEW}} is given, then the file-descriptor {{NEW}} is opened
466to access the file with the file-descriptor {{OLD}}. Otherwise a
467fresh file-descriptor accessing the same file as {{OLD}} is returned.
468
469==== file-close
470
471<procedure>(file-close FILENO)</procedure>
472
473Closes the input/output file with the file-descriptor {{FILENO}}.
474
475==== file-open
476
477<procedure>(file-open FILENAME FLAGS [MODE])</procedure>
478
479Opens the file specified with the string {{FILENAME}} and open-flags
480{{FLAGS}} using the C function {{open(2)}}. On success a
481file-descriptor for the opened file is returned.
482
483{{FLAGS}} is a bitmask of {{open/...}}
484values '''or'''ed together using {{bitwise-ior}} (or simply added
485together). You must provide exactly one of the access flags {{open/rdonly}}, {{open/wronly}}, or {{open/rdwr}}. Additionally, you may provide zero or more creation flags ({{open/creat}}, {{open/excl}}, {{open/trunc}}, and {{open/noctty}}) and status flags (the remaining {{open/...}} values). For example, to open a possibly new output file for appending:
486
487 (file-open "/tmp/hen.txt" (+ open/wronly open/append open/creat))
488
489The optional {{MODE}} should be a bitmask composed of one
490or more permission values like {{perm/irusr}} and is only relevant
491when a new file is created. The default mode is
492{{perm/irwxu | perm/irgrp | perm/iroth}}.
493
494==== file-mkstemp
495
496<procedure>(file-mkstemp TEMPLATE-FILENAME)</procedure>
497
498Create a file based on the given {{TEMPLATE-FILENAME}}, in which
499the six last characters must be ''XXXXXX''. These will be replaced
500with a string that makes the filename unique. The file descriptor of
501the created file and the generated filename is returned. See the
502{{mkstemp(3)}} manual page for details on how this function
503works. The template string given is not modified.
504
505Example usage:
506
507<enscript highlight=scheme>
508 (let-values (((fd temp-path) (file-mkstemp "/tmp/mytemporary.XXXXXX")))
509 (let ((temp-port (open-output-file* fd)))
510 (format temp-port "This file is ~A.~%" temp-path)
511 (close-output-port temp-port)))
512</enscript>
513
514==== file-read
515
516<procedure>(file-read FILENO SIZE [BUFFER])</procedure>
517
518Reads {{SIZE}} bytes from the file with the file-descriptor
519{{FILENO}}. If a string or bytevector is passed in the optional
520argument {{BUFFER}}, then this string will be destructively modified
521to contain the read data. This procedure returns a list with two values:
522the buffer containing the data and the number of bytes read.
523
524==== file-select
525
526<procedure>(file-select READFDLIST WRITEFDLIST [TIMEOUT])</procedure>
527
528Waits until any of the file-descriptors given in the lists
529{{READFDLIST}} and {{WRITEFDLIST}} is ready for input or
530output, respectively. If the optional argument {{TIMEOUT}} is
531given and not false, then it should specify the number of seconds after
532which the wait is to be aborted (the value may be a float). This procedure returns two values:
533the lists of file-descriptors ready for input and output, respectively.
534{{READFDLIST}} and {{WRITEFDLIST}} may also be file-descriptors
535instead of lists. In this case the returned values are booleans
536indicating whether input/output is ready by {{#t}} or {{#f}}
537otherwise. You can also pass {{#f}} as {{READFDLIST}} or
538{{WRITEFDLIST}} argument, which is equivalent to {{()}}.
539
540'''NOTE''': On native Windows builds (all except cygwin), this
541procedure is unimplemented and will raise an error.
542
543==== file-write
544
545<procedure>(file-write FILENO BUFFER [SIZE])</procedure>
546
547Writes the contents of the string or bytevector {{BUFFER}} into
548the file with the file-descriptor {{FILENO}}. If the optional
549argument {{SIZE}} is given, then only the specified number of bytes
550are written.
551
552==== file-control
553
554<procedure>(file-control FILENO COMMAND [ARGUMENT])</procedure>
555
556Performs the fcntl operation {{COMMAND}} with the given
557{{FILENO}} and optional {{ARGUMENT}}. The return value is
558meaningful depending on the {{COMMAND}}.
559
560'''NOTE''': On native Windows builds (all except cygwin), this
561procedure is unimplemented and will raise an error.
562
563==== open-input-file*
564==== open-output-file*
565
566<procedure>(open-input-file* FILENO [OPENMODE])</procedure><br>
567<procedure>(open-output-file* FILENO [OPENMODE])</procedure>
568
569Opens file for the file-descriptor {{FILENO}} for input or output
570and returns a port. {{FILENO}} should be a positive exact integer.
571{{OPENMODE}} specifies an additional mode for opening the file
572(currently only the keyword {{#:append}} is supported, which opens
573an output-file for appending).
574
575==== port->fileno
576
577<procedure>(port->fileno PORT)</procedure>
578
579If {{PORT}} is a file- or tcp-port, then a file-descriptor is returned for
580this port. Otherwise an error is signaled.
581
582
583=== Record locking
584
585These procedures are all unsupported on native Windows builds (all
586except cygwin).
587
588==== file-lock
589
590<procedure>(file-lock PORT [START [LEN]])</procedure>
591
592Locks the file associated with {{PORT}} for reading or
593writing (according to whether {{PORT}} is an input- or
594output-port). {{START}} specifies the starting position in the
595file to be locked and defaults to 0. {{LEN}} specifies the length
596of the portion to be locked and defaults to {{#t}}, which means
597the complete file. {{file-lock}} returns a ''lock''-object.
598
599'''NOTE''': On native Windows builds (all except cygwin), this
600procedure is unimplemented and will raise an error.
601
602==== file-lock/blocking
603
604<procedure>(file-lock/blocking PORT [START [LEN]])</procedure>
605
606Similar to {{file-lock}}, but if a lock is held on the file,
607the current process blocks (including all threads) until the lock is released.
608
609'''NOTE''': On native Windows builds (all except cygwin), this
610procedure is unimplemented and will raise an error.
611
612==== file-test-lock
613
614<procedure>(file-test-lock PORT [START [LEN]])</procedure>
615
616Tests whether the file associated with {{PORT}} is locked for reading
617or writing (according to whether {{PORT}} is an input- or output-port)
618and returns either {{#f}} or the process-id of the locking process.
619
620'''NOTE''': On native Windows builds (all except cygwin), this
621procedure is unimplemented and will raise an error.
622
623==== file-unlock
624
625<procedure>(file-unlock LOCK)</procedure>
626
627Unlocks the previously locked portion of a file given in {{LOCK}}.
628
629'''NOTE''': On native Windows builds (all except cygwin), this
630procedure is unimplemented and will raise an error.
631
632---
633Previous: [[Module (chicken file)]]
634
635Next: [[Module (chicken fixnum)]]
636